package com.aop.controller;


import com.aop.pojo.Emp;
import com.aop.pojo.PageBean;
import com.aop.pojo.Result;
import com.aop.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author wzm
 * @create 2023-10-07 16:48
 * project_name:stu_sys
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
   private EmpService empService;

    /*原生的分页*/
    /*   @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询，参数{},{}",page,pageSize);
        //调用service方法查询分页数据
        PageBean pageBean = empService.page(page, pageSize);
        //返回的数据包含了当前页的数据+总记录数
        return Result.success(pageBean);
    }*/


    //使用pageHelper
    @GetMapping
    public Result page(String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                        @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询，参数{},{},{},{},{},{}",name,gender,begin,end,page,pageSize);
        //调用service方法查询分页数据
        PageBean pageBean = empService.page(name,gender,begin,end,page, pageSize);
        //返回的数据包含了当前页的数据+总记录数
        return Result.success(pageBean);

    }



    //删除员工
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        empService.delete(ids);
        return Result.success();
    }

    //新增员工
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工,{}",emp);
        //调用业务层新增
        empService.save(emp);
        //响应结果
        return Result.success();

    }

    //根据id查询员工的信息、
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询员工信息：{}",id);

        Emp emp = empService.findById(id);
        return  Result.success(emp);
    }


    //根据员工id修改员工信息
    @PutMapping
    public Result upate(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();

    }





}